package edu.cwnu.enterprisemanagement.controller;

import edu.cwnu.enterprisemanagement.DTO.LoginDTO;
import edu.cwnu.enterprisemanagement.VO.AdminVO;
import edu.cwnu.enterprisemanagement.common.Result;
import edu.cwnu.enterprisemanagement.domain.Admin;
import edu.cwnu.enterprisemanagement.exception.BusinessException;
import edu.cwnu.enterprisemanagement.exception.GlobalException;
import edu.cwnu.enterprisemanagement.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@Tag(name = "管理员接口")
@Slf4j
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    @Operation(summary = "管理员登录")
    public Result<AdminVO> login(@RequestBody LoginDTO loginDTO) {
        if (loginDTO.getUsername() == null || loginDTO.getUsername().isEmpty()) {
            throw new BusinessException(1001,
                    GlobalException.USER_NAME_EMPTY);
        }
        
        if (loginDTO.getPassword() == null || loginDTO.getPassword().isEmpty()) {
            throw new BusinessException(1002,
                    GlobalException.USER_PASSWORD_EMPTY);
        }
        
        AdminVO adminVo = adminService.login(
                loginDTO.getUsername(), loginDTO.getPassword());
        if (adminVo != null) {
            return Result.success(adminVo);
        }
        return Result.error(GlobalException
                .USERNAME_OR_PASSWORD_ERROR);
    }

    @GetMapping("/list")
    @Operation(summary = "管理员列表")
    public Result<List<Admin>> list() {
        return Result.success(adminService.list());
    }
}